package xyz.ibenben.zhongdian.system.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import xyz.ibenben.zhongdian.common.annotation.SystemControllerLog;
import xyz.ibenben.zhongdian.common.constants.Constants;
import xyz.ibenben.zhongdian.system.entity.HouseStatus;
import xyz.ibenben.zhongdian.system.entity.OtherRightDigest;
import xyz.ibenben.zhongdian.system.entity.PropertyOwnershipCertificate;
import xyz.ibenben.zhongdian.system.form.PropertyOwnershipCertificateForm;
import xyz.ibenben.zhongdian.system.service.HouseStatusService;
import xyz.ibenben.zhongdian.system.service.OtherRightDigestService;
import xyz.ibenben.zhongdian.system.service.PropertyOwnershipCertificateService;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 房产证控制类
 * 提供了一些基本的服务，如初始化、获取房产证列表等方法。
 * spring mvc思想 通过controller来调用service里的方法，
 * service里的方法再通过调用Dao来实现对数据的操作
 * 返回值统统是String类型的返回页面有统一的安排方便代码提取
 *
 * @author chenjian
 * @since 2017年9月27日上午10:57:28
 */
@Controller
@SessionAttributes("propertyOwnershipCertificateForm")
@RequestMapping("/propertyOwnershipCertificate")
public class PropertyOwnershipCertificateController extends BaseController{
	/* 实体名称 */
	private static final String ENTITYNAME = "propertyOwnershipCertificate";

	private final PropertyOwnershipCertificateService propertyOwnershipCertificateService;
	private final HouseStatusService houseStatusService;
	private final OtherRightDigestService otherRightDigestService;

	@Autowired
	public PropertyOwnershipCertificateController(PropertyOwnershipCertificateService propertyOwnershipCertificateService, HouseStatusService houseStatusService, OtherRightDigestService otherRightDigestService) {
		this.propertyOwnershipCertificateService = propertyOwnershipCertificateService;
		this.houseStatusService = houseStatusService;
		this.otherRightDigestService = otherRightDigestService;
	}

	/**
	 * 初始化
	 * @param map 参数
	 * @param propertyOwnershipCertificateForm 条件参数
	 */
	@ModelAttribute
	public void init(Model map, @ModelAttribute PropertyOwnershipCertificateForm propertyOwnershipCertificateForm){
		PropertyOwnershipCertificateForm newPropertyOwnershipCertificateForm = new PropertyOwnershipCertificateForm();
		if(propertyOwnershipCertificateForm != null){
			newPropertyOwnershipCertificateForm = propertyOwnershipCertificateForm;
		}
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newPropertyOwnershipCertificateForm);
	}

	/**
	 * 获取房产证列表
	 * @param map 参数
	 * @param propertyOwnershipCertificateForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/getall")
	@SystemControllerLog(description = "获取房产证列表")
	public String getall(Model map, @ModelAttribute PropertyOwnershipCertificateForm propertyOwnershipCertificateForm){
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), propertyOwnershipCertificateForm);
		Page<PropertyOwnershipCertificate> page = PageHelper.startPage(propertyOwnershipCertificateForm.getPageIndex(), 
				propertyOwnershipCertificateForm.getPageSize());
		List<PropertyOwnershipCertificate> list = propertyOwnershipCertificateService.selectByExample(
				propertyOwnershipCertificateForm.buildSearchCommond());
		map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), list);
		map.addAttribute(Constants.PI, page.toPageInfo());
		return String.format(Constants.LISTPAGE, ENTITYNAME);
	}

	/**
	 * 删除房产证记录
	 * @param id 房产证主键
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value = "/{id}/delete")
	@SystemControllerLog(description = "删除房产证记录")
	public String delete(@PathVariable Long id, HttpServletRequest request) {
		propertyOwnershipCertificateService.delete(id, request);
		return String.format(Constants.REDIRECT, ENTITYNAME);
	}

	/**
	 * 删除房产证列表
	 * @param propertyOwnershipCertificateForm 条件参数
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping("/deleteall")
	@SystemControllerLog(description = "删除房产证列表")
	public String deleteall(@ModelAttribute PropertyOwnershipCertificateForm propertyOwnershipCertificateForm,
							HttpServletRequest request) {
		if(propertyOwnershipCertificateForm.getSelectIds() != null){
			String[] ids = propertyOwnershipCertificateForm.getSelectIds().split(Constants.COMMA);
			for(String id: ids){
				propertyOwnershipCertificateService.delete(Long.parseLong(id), request);
			}
		}
		return String.format(Constants.REDIRECT, ENTITYNAME);
	}

	/**
	 * 打开房产证添加页面
	 * @param map 参数
	 * @return 页面
	 */
	@RequestMapping(value="/add", method= RequestMethod.GET)
	@SystemControllerLog(description = "打开房产证添加页面")
	public String openAddPage(Model map){
		map.addAttribute(ENTITYNAME, new PropertyOwnershipCertificate());
		return String.format(Constants.ADDPAGE, ENTITYNAME);
	}

	/**
	 * 保存房产证添加记录
	 * @param t 实体
	 * @param bindingResult 校验
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value="/add", method= RequestMethod.POST)
	@SystemControllerLog(description = "保存房产证添加记录")
	public String postAddMessage(@Valid @ModelAttribute(ENTITYNAME) PropertyOwnershipCertificate t,
			BindingResult bindingResult, HttpServletRequest request){
		if (!bindingResult.hasErrors()) {
			propertyOwnershipCertificateService.save(t, request);
			return String.format(Constants.REDIRECT, ENTITYNAME);
		}
		return String.format(Constants.ADDPAGE, ENTITYNAME);
	}

	/**
	 * 打开房产证修改页面
	 * @param map 参数
	 * @param id 房产证主键
	 * @return 页面
	 */
	@RequestMapping(value="/{id}/edit", method= RequestMethod.GET)
	@SystemControllerLog(description = "打开房产证修改页面")
	public String openEditPage(Model map, @PathVariable Long id){
		map.addAttribute(ENTITYNAME, propertyOwnershipCertificateService.selectByKey(id));
		return String.format(Constants.EDITPAGE, ENTITYNAME);
	}

	/**
	 * 更新房产证修改记录
	 * @param t 实体
	 * @param bindingResult 校验
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value="/edit", method= RequestMethod.POST)
	@SystemControllerLog(description = "更新房产证修改记录")
	public String postEditMessage(@Valid @ModelAttribute(ENTITYNAME) PropertyOwnershipCertificate t,
			BindingResult bindingResult, HttpServletRequest request){
		if (!bindingResult.hasErrors()) {
			propertyOwnershipCertificateService.updateNotNull(t, request);
			return String.format(Constants.REDIRECT, ENTITYNAME);
		}
		return String.format(Constants.EDITPAGE, ENTITYNAME);
	}

	/**
	 * 浏览房产证记录
	 * @param map 参数
	 * @param id 房产证主键
	 * @param propertyOwnershipCertificateForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/{id}/view", method= RequestMethod.GET)
	@SystemControllerLog(description = "浏览房产证记录")
	public String openViewPage(Model map, @PathVariable Long id,
							   @ModelAttribute PropertyOwnershipCertificateForm propertyOwnershipCertificateForm){
		PropertyOwnershipCertificate info = propertyOwnershipCertificateService.selectByKey(id);
		map.addAttribute(ENTITYNAME, info);
		List<HouseStatus> statusList = houseStatusService.findByOwnerId(info.getId());
		map.addAttribute("statusList", statusList);
		List<OtherRightDigest> digestList = otherRightDigestService.findByOwnerId(info.getId());
		map.addAttribute("digestList", digestList);
		return String.format(Constants.VIEWPAGE, ENTITYNAME);
	}
	
}

